//
//  FeedBackControllerViewController.m
//  FootPrint
//
//  Created by 秦 东方 on 4/26/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "FeedBackControllerViewController.h"

@interface FeedBackControllerViewController ()

@end

@implementation FeedBackControllerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _parser = [[FeedBackParser alloc]init];
        [_parser setDelegate:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage* bg = [[UIImage imageNamed:@"Button_Single.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    _imgTextBg.image = bg;
    
    
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
    
    [_textView setDelegate:self];
    
    UIImage* imgNormal =[[UIImage imageNamed:@"btn_bg_normal.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    UIImage* imgPress =[[UIImage imageNamed:@"btn_bg_press.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    [_btnSubmit setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnSubmit setBackgroundImage:imgPress forState:UIControlStateHighlighted];
    
}

- (void)didFinishFeedBack:(UserFeedbackResponseMessage*)feedBackMsg{

    if(feedBackMsg != nil && [feedBackMsg resultCode] == 0){
         [[[iToast makeText:@"提交成功，谢谢您的建议"] setDuration:2000] show];
        if (self.navigationController.viewControllers.count > 1) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self.navigationController dismissModalViewControllerAnimated:YES];
        }
        return;
    }
     [[[iToast makeText:@"服务器出错"] setDuration:2000] show];
}

- (void)failFeedBack{

     [[[iToast makeText:@"服务器出错"] setDuration:2000] show];
}


- (void) viewTapped:(UITapGestureRecognizer*)tapGr{
    
    [_textView resignFirstResponder];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if([text isEqualToString:@"\n"]){
        [_textView resignFirstResponder];
    }
    return YES;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}
- (void)dealloc {
    [_imgTextBg release];
    [_textView release];
    [_btnSubmit release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setImgTextBg:nil];
    [self setTextView:nil];
    [self setBtnSubmit:nil];
    [super viewDidUnload];
}
- (IBAction)actionSubmit:(id)sender {
    
    NSString* cont = _textView.text;
    [_parser reqFeedBack:[GConfig getUserId] content:cont];
}
@end
